package com.fomky.queried.plugin.common;

import com.fomky.queried.common.connect.RowDataList;
import com.fomky.queried.common.entity.QueryRequest;

import java.util.List;
import java.util.UUID;

public interface ConnectPlugin {

    default String connectId() {
        return UUID.randomUUID().toString();
    }

    String name();

    default String lastUseTime() {
        return UUID.randomUUID().toString();
    }
    default boolean isTimeout() {
        return false;
    }

    default void updateUseTime() {}

    ConnectPlugin init(ConnectConfig config);

    RowDataList catalogs();

    RowDataList databases(String catalog);

    RowDataList tables(String catalog, String database);

    RowDataList tableInfo(String catalog, String database, String table);

    RowDataList execute(String sql);

    default RowDataList executeRoute(QueryRequest request) {
        this.updateUseTime();
        switch (request.getAction()) {
            case CATALOG -> {
                return this.catalogs();
            }
            case TABLE -> {
                return this.tables(request.getCatalog(), request.getDatabase());
            }
            case DATABASE -> {
                return this.databases(request.getCatalog());
            }
            case TABLE_DATA -> {
                return  this.execute(request.getExecuteSql());
            }
            case COLUMN -> {
                return this.tableInfo(request.getCatalog(), request.getDatabase(), request.getTable());
            }
            default -> {
                return RowDataList.of(List.of());
            }
        }
    }
}
